home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevprn.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  37.3 KB  |  1,242 lines

  1. /* Copyright (C) 1990, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevprn.c,v 1.5 2000/09/19 19:00:21 lpd Exp $ */
  20. /* Generic printer driver support */
  21. #include "ctype_.h"
  22. #include "gdevprn.h"
  23. #include "gp.h"
  24. #include "gsdevice.h"        /* for gs_deviceinitialmatrix */
  25. #include "gsfname.h"
  26. #include "gsparam.h"
  27. #include "gxclio.h"
  28. #include "gxgetbit.h"
  29. #include "gdevplnx.h"
  30.  
  31. /*#define DEBUGGING_HACKS*/
  32.  
  33. /* GC information */
  34. #define PRINTER_IS_CLIST(pdev) ((pdev)->buffer_space != 0)
  35. private
  36. ENUM_PTRS_WITH(device_printer_enum_ptrs, gx_device_printer *pdev)
  37.     if (PRINTER_IS_CLIST(pdev))
  38.     ENUM_PREFIX(st_device_clist, 0);
  39.     else
  40.     ENUM_PREFIX(st_device_forward, 0);
  41. ENUM_PTRS_END
  42. private
  43. RELOC_PTRS_WITH(device_printer_reloc_ptrs, gx_device_printer *pdev)
  44. {
  45.     if (PRINTER_IS_CLIST(pdev))
  46.     RELOC_PREFIX(st_device_clist);
  47.     else
  48.     RELOC_PREFIX(st_device_forward);
  49. } RELOC_PTRS_END
  50. public_st_device_printer();
  51.  
  52. /* ---------------- Standard device procedures ---------------- */
  53.  
  54. /* Define the standard printer procedure vector. */
  55. const gx_device_procs prn_std_procs =
  56.     prn_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close);
  57.  
  58. /* Forward references */
  59. int gdev_prn_maybe_realloc_memory(P4(gx_device_printer *pdev, 
  60.                      gdev_prn_space_params *old_space,
  61.                      int old_width, int old_height));
  62.  
  63. /* ------ Open/close ------ */
  64.  
  65. /* Open a generic printer device. */
  66. /* Specific devices may wish to extend this. */
  67. int
  68. gdev_prn_open(gx_device * pdev)
  69. {
  70.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  71.     int code;
  72.  
  73.     ppdev->file = NULL;
  74.     code = gdev_prn_allocate_memory(pdev, NULL, 0, 0);
  75.     if (code < 0)
  76.     return code;
  77.     if (ppdev->OpenOutputFile)
  78.     code = gdev_prn_open_printer(pdev, 1);
  79.     return code;
  80. }
  81.  
  82. /* Generic closing for the printer device. */
  83. /* Specific devices may wish to extend this. */
  84. int
  85. gdev_prn_close(gx_device * pdev)
  86. {
  87.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  88.     int code = 0;
  89.  
  90.     gdev_prn_free_memory(pdev);
  91.     if (ppdev->file != NULL) {
  92.     code = gx_device_close_output_file(pdev, ppdev->fname, ppdev->file);
  93.     ppdev->file = NULL;
  94.     }
  95.     return code;
  96. }
  97.  
  98. private int        /* returns 0 ok, else -ve error cde */
  99. gdev_prn_setup_as_command_list(gx_device *pdev, gs_memory_t *buffer_memory,
  100.                    byte **the_memory,
  101.                    const gdev_prn_space_params *space_params,
  102.                    bool bufferSpace_is_exact)
  103. {
  104.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  105.     uint space;
  106.     int code;
  107.     gx_device_clist *const pclist_dev = (gx_device_clist *)pdev;
  108.     gx_device_clist_common * const pcldev = &pclist_dev->common;
  109.     bool reallocate = *the_memory != 0;
  110.     byte *base;
  111.  
  112.     /* Try to allocate based simply on param-requested buffer size */
  113. #ifdef DEBUGGING_HACKS
  114. #define BACKTRACE(first_arg)\
  115.   BEGIN\
  116.     ulong *fp_ = (ulong *)&first_arg - 2;\
  117.     for (; fp_ && (fp_[1] & 0xff000000) == 0x08000000; fp_ = (ulong *)*fp_)\
  118.     dprintf2("  fp=0x%lx ip=0x%lx\n", (ulong)fp_, fp_[1]);\
  119.   END
  120. dputs("alloc buffer:\n");
  121. BACKTRACE(pdev);
  122. #endif /*DEBUGGING_HACKS*/
  123.     for ( space = space_params->BufferSpace; ; ) {
  124.     base = (reallocate ?
  125.         (byte *)gs_resize_object(buffer_memory, *the_memory, space,
  126.                      "cmd list buffer") :
  127.         gs_alloc_bytes(buffer_memory, space,
  128.                    "cmd list buffer"));
  129.     if (base != 0)
  130.         break;
  131.     if (bufferSpace_is_exact || (space >>= 1) < PRN_MIN_BUFFER_SPACE)
  132.         break;
  133.     }
  134.     if (base == 0)
  135.     return_error(gs_error_VMerror);
  136.     *the_memory = base;
  137.  
  138.     /* Try opening the command list, to see if we allocated */
  139.     /* enough buffer space. */
  140. open_c:
  141.     ppdev->buf = base;
  142.     ppdev->buffer_space = space;
  143.     clist_init_params(pclist_dev, base, space, pdev,
  144.               ppdev->printer_procs.buf_procs,
  145.               space_params->band, ppdev->is_async_renderer,
  146.               (ppdev->bandlist_memory == 0 ? &gs_memory_default :
  147.                ppdev->bandlist_memory),
  148.               ppdev->free_up_bandlist_memory,
  149.               ppdev->clist_disable_mask);
  150.     code = (*gs_clist_device_procs.open_device)( (gx_device *)pcldev );
  151.     if (code < 0) {
  152.     /* If there wasn't enough room, and we haven't */
  153.     /* already shrunk the buffer, try enlarging it. */
  154.     if ( code == gs_error_limitcheck &&
  155.          space >= space_params->BufferSpace &&
  156.          !bufferSpace_is_exact
  157.          ) {
  158.         space <<= 1;
  159.         if (reallocate) {
  160.         base = gs_resize_object(buffer_memory, 
  161.                     *the_memory, space,
  162.                     "cmd list buf(retry open)");
  163.         if (base != 0)
  164.             *the_memory = base;
  165.         } else {
  166.         gs_free_object(buffer_memory, base,
  167.                    "cmd list buf(retry open)");
  168.         *the_memory = base =
  169.             gs_alloc_bytes(buffer_memory, space,
  170.                    "cmd list buf(retry open)");
  171.         }
  172.         ppdev->buf = *the_memory;
  173.         if (base != 0)
  174.         goto open_c;
  175.     }
  176.     /* Failure. */
  177.     if (!reallocate) {
  178.         gs_free_object(buffer_memory, base, "cmd list buf");
  179.         ppdev->buffer_space = 0;
  180.         *the_memory = 0;
  181.     }
  182.     }
  183.     return code;
  184. }
  185.  
  186. private bool    /* ret true if device was cmd list, else false */
  187. gdev_prn_tear_down(gx_device *pdev, byte **the_memory)
  188. {
  189.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  190.     gx_device_memory * const pmemdev = (gx_device_memory *)pdev;
  191.     gx_device_clist *const pclist_dev = (gx_device_clist *)pdev;
  192.     gx_device_clist_common * const pcldev = &pclist_dev->common;
  193.     bool is_command_list;
  194.  
  195.     if (ppdev->buffer_space != 0) {
  196.     /* Close cmd list device & point to the storage */
  197.     (*gs_clist_device_procs.close_device)( (gx_device *)pcldev );
  198.     *the_memory = ppdev->buf;
  199.     ppdev->buf = 0;
  200.     ppdev->buffer_space = 0;
  201.     is_command_list = true;
  202.     } else {
  203.     /* point at the device bitmap, no need to close mem dev */
  204.     *the_memory = pmemdev->base;
  205.     pmemdev->base = 0;
  206.     is_command_list = false;
  207.     }
  208.  
  209.     /* Reset device proc vector to default */
  210.     if (ppdev->orig_procs.open_device != 0)
  211.     pdev->procs = ppdev->orig_procs;
  212.     ppdev->orig_procs.open_device = 0;    /* prevent uninit'd restore of procs */
  213.  
  214.     return is_command_list;
  215. }
  216.  
  217. private int
  218. gdev_prn_allocate(gx_device *pdev, gdev_prn_space_params *new_space_params,
  219.           int new_width, int new_height, bool reallocate)
  220. {
  221.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  222.     gx_device_memory * const pmemdev = (gx_device_memory *)pdev;
  223.     byte *the_memory = 0;
  224.     gdev_prn_space_params save_params;
  225.     int save_width, save_height;
  226.     bool is_command_list;
  227.     bool save_is_command_list;
  228.     int ecode = 0;
  229.     int pass;
  230.     gs_memory_t *buffer_memory =
  231.     (ppdev->buffer_memory == 0 ? &gs_memory_default :
  232.      ppdev->buffer_memory);
  233.  
  234.     /* If reallocate, find allocated memory & tear down buffer device */
  235.     if (reallocate)
  236.     save_is_command_list = gdev_prn_tear_down(pdev, &the_memory);
  237.  
  238.     /* Re/allocate memory */
  239.     ppdev->orig_procs = pdev->procs;
  240.     for ( pass = 1; pass <= (reallocate ? 2 : 1); ++pass ) {
  241.     ulong mem_space;
  242.     byte *base = 0;
  243.     bool bufferSpace_is_default = false;
  244.     gdev_prn_space_params space_params;
  245.     gx_device_buf_space_t buf_space;
  246.  
  247.     if (reallocate)
  248.         switch (pass)
  249.             {
  250.         case 1:
  251.             /* Setup device to get reallocated */
  252.             save_params = ppdev->space_params;
  253.             ppdev->space_params = *new_space_params;
  254.             save_width = ppdev->width;
  255.             ppdev->width = new_width;
  256.             save_height = ppdev->height;
  257.             ppdev->height = new_height;
  258.             break;
  259.         case 2:    /* only comes here if reallocate */
  260.             /* Restore device to previous contents */
  261.             ppdev->space_params = save_params;
  262.             ppdev->width = save_width;
  263.             ppdev->height = save_height;
  264.             break;
  265.             }
  266.  
  267.     /* Init clist/mem device-specific fields */
  268.     memset(ppdev->skip, 0, sizeof(ppdev->skip));
  269.     ppdev->printer_procs.buf_procs.size_buf_device
  270.         (&buf_space, pdev, NULL, pdev->height, false);
  271.     mem_space = buf_space.bits + buf_space.line_ptrs;
  272.  
  273.     /* Compute desired space params: never use the space_params as-is. */
  274.     /* Rather, give the dev-specific driver a chance to adjust them. */
  275.     space_params = ppdev->space_params;
  276.     space_params.BufferSpace = 0;
  277.     (*ppdev->printer_procs.get_space_params)(ppdev, &space_params);
  278.     if (ppdev->is_async_renderer && space_params.band.BandBufferSpace != 0)
  279.         space_params.BufferSpace = space_params.band.BandBufferSpace;
  280.     else if (space_params.BufferSpace == 0) {
  281.         if (space_params.band.BandBufferSpace > 0)
  282.             space_params.BufferSpace = space_params.band.BandBufferSpace;
  283.         else {
  284.         space_params.BufferSpace = ppdev->space_params.BufferSpace;
  285.         bufferSpace_is_default = true;
  286.         }
  287.     }
  288.  
  289.     /* Determine if we can use a full bitmap buffer, or have to use banding */
  290.     if (pass > 1)
  291.         is_command_list = save_is_command_list;
  292.     else {
  293.         is_command_list = space_params.banding_type == BandingAlways ||
  294.         mem_space >= space_params.MaxBitmap ||
  295.         mem_space != (uint)mem_space;        /* too big to allocate */
  296.     }
  297.     if (!is_command_list) {
  298.         /* Try to allocate memory for full memory buffer */
  299.         base =
  300.         (reallocate ?
  301.          (byte *)gs_resize_object(buffer_memory, the_memory,
  302.                       (uint)mem_space, "printer buffer") :
  303.          gs_alloc_bytes(buffer_memory, (uint)mem_space,
  304.                 "printer_buffer"));
  305.         if (base == 0)
  306.         is_command_list = true;
  307.         else
  308.         the_memory = base;
  309.     }
  310.     if (!is_command_list && pass == 1 && PRN_MIN_MEMORY_LEFT != 0
  311.         && buffer_memory == &gs_memory_default) {
  312.         /* before using full memory buffer, ensure enough working mem left */
  313.         byte * left = gs_alloc_bytes( buffer_memory,
  314.                       PRN_MIN_MEMORY_LEFT, "printer mem left");
  315.         if (left == 0)
  316.         is_command_list = true;
  317.         else
  318.         gs_free_object(buffer_memory, left, "printer mem left");
  319.     }
  320.  
  321.     if (is_command_list) {
  322.         /* Buffer the image in a command list. */
  323.         /* Release the buffer if we allocated it. */
  324.         int code;
  325.         if (!reallocate) {
  326.         gs_free_object(buffer_memory, the_memory,
  327.                    "printer buffer(open)");
  328.         the_memory = 0;
  329.         }
  330.         if (space_params.banding_type == BandingNever) {
  331.         ecode = gs_note_error(gs_error_VMerror);
  332.         continue;
  333.         }
  334.         code = gdev_prn_setup_as_command_list(pdev, buffer_memory,
  335.                           &the_memory, &space_params,
  336.                           !bufferSpace_is_default);
  337.         if (ecode == 0)
  338.         ecode = code;
  339.  
  340.         if ( code >= 0 || (reallocate && pass > 1) )
  341.         ppdev->procs = gs_clist_device_procs;
  342.     } else {
  343.         /* Render entirely in memory. */
  344.         gx_device *bdev = (gx_device *)pmemdev;
  345.         int code;
  346.  
  347.         ppdev->buffer_space = 0;
  348.         if ((code = gdev_create_buf_device
  349.          (ppdev->printer_procs.buf_procs.create_buf_device,
  350.           &bdev, pdev, NULL, NULL, false)) < 0 ||
  351.         (code = ppdev->printer_procs.buf_procs.setup_buf_device
  352.          (bdev, base, buf_space.raster,
  353.           (byte **)(base + buf_space.bits), 0, pdev->height,
  354.           pdev->height)) < 0
  355.         ) {
  356.         /* Catastrophic. Shouldn't ever happen */
  357.         gs_free_object(buffer_memory, base, "printer buffer");
  358.         pdev->procs = ppdev->orig_procs;
  359.         ppdev->orig_procs.open_device = 0;    /* prevent uninit'd restore of procs */
  360.         return_error(code);
  361.         }
  362.         pmemdev->base = base;
  363.     }
  364.     if (ecode == 0)
  365.         break;
  366.     }
  367.  
  368.     if (ecode >= 0 || reallocate) {    /* even if realloc failed */
  369.     /* Synthesize the procedure vector. */
  370.     /* Rendering operations come from the memory or clist device, */
  371.     /* non-rendering come from the printer device. */
  372. #define COPY_PROC(p) set_dev_proc(ppdev, p, ppdev->orig_procs.p)
  373.     COPY_PROC(get_initial_matrix);
  374.     COPY_PROC(output_page);
  375.     COPY_PROC(close_device);
  376.     COPY_PROC(map_rgb_color);
  377.     COPY_PROC(map_color_rgb);
  378.     COPY_PROC(get_params);
  379.     COPY_PROC(put_params);
  380.     COPY_PROC(map_cmyk_color);
  381.     COPY_PROC(get_xfont_procs);
  382.     COPY_PROC(get_xfont_device);
  383.     COPY_PROC(map_rgb_alpha_color);
  384.     /* All printers are page devices, even if they didn't use the */
  385.     /* standard macros for generating their procedure vectors. */
  386.     set_dev_proc(ppdev, get_page_device, gx_page_device_get_page_device);
  387.     COPY_PROC(get_clipping_box);
  388.     COPY_PROC(map_color_rgb_alpha);
  389.     COPY_PROC(get_hardware_params);
  390. #undef COPY_PROC
  391.     /* If using a command list, already opened the device. */
  392.     if (is_command_list)
  393.         return ecode;
  394.     else
  395.         return (*dev_proc(pdev, open_device))(pdev);
  396.     } else {
  397.     pdev->procs = ppdev->orig_procs;
  398.     ppdev->orig_procs.open_device = 0;    /* prevent uninit'd restore of procs */
  399.     return ecode;
  400.     }
  401. }
  402.  
  403. int
  404. gdev_prn_allocate_memory(gx_device *pdev,
  405.              gdev_prn_space_params *new_space_params,
  406.              int new_width, int new_height)
  407. {
  408.     return gdev_prn_allocate(pdev, new_space_params,
  409.                  new_width, new_height, false);
  410. }
  411.  
  412. int
  413. gdev_prn_reallocate_memory(gx_device *pdev,
  414.              gdev_prn_space_params *new_space_params,
  415.              int new_width, int new_height)
  416. {
  417.     return gdev_prn_allocate(pdev, new_space_params,
  418.                  new_width, new_height, true);
  419. }
  420.  
  421. int
  422. gdev_prn_free_memory(gx_device *pdev)
  423. {
  424.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  425.     byte *the_memory = 0;
  426.     gs_memory_t *buffer_memory =
  427.     (ppdev->buffer_memory == 0 ? &gs_memory_default :
  428.      ppdev->buffer_memory);
  429.  
  430.     gdev_prn_tear_down(pdev, &the_memory);
  431.     gs_free_object(buffer_memory, the_memory, "gdev_prn_free_memory");
  432.     return 0;
  433. }
  434.  
  435. /* ------------- Stubs related only to async rendering ------- */
  436.  
  437. int    /* rets 0 ok, -ve error if couldn't start thread */
  438. gx_default_start_render_thread(gdev_prn_start_render_params *params)
  439. {
  440.     return gs_error_unknownerror;
  441. }
  442.  
  443. /* Open the renderer's copy of a device. */
  444. /* This is overriden in gdevprna.c */
  445. int
  446. gx_default_open_render_device(gx_device_printer *pdev)
  447. {
  448.     return gs_error_unknownerror;
  449. }
  450.  
  451. /* Close the renderer's copy of a device. */
  452. int
  453. gx_default_close_render_device(gx_device_printer *pdev)
  454. {
  455.     return gdev_prn_close( (gx_device *)pdev );
  456. }
  457.  
  458. /* ------ Get/put parameters ------ */
  459.  
  460. /* Get parameters.  Printer devices add several more parameters */
  461. /* to the default set. */
  462. int
  463. gdev_prn_get_params(gx_device * pdev, gs_param_list * plist)
  464. {
  465.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  466.     int code = gx_default_get_params(pdev, plist);
  467.     gs_param_string ofns;
  468.  
  469.     if (code < 0 ||
  470.     (code = param_write_long(plist, "MaxBitmap", &ppdev->space_params.MaxBitmap)) < 0 ||
  471.     (code = param_write_long(plist, "BufferSpace", &ppdev->space_params.BufferSpace)) < 0 ||
  472.     (code = param_write_int(plist, "BandWidth", &ppdev->space_params.band.BandWidth)) < 0 ||
  473.     (code = param_write_int(plist, "BandHeight", &ppdev->space_params.band.BandHeight)) < 0 ||
  474.     (code = param_write_long(plist, "BandBufferSpace", &ppdev->space_params.band.BandBufferSpace)) < 0 ||
  475.     (code = param_write_bool(plist, "OpenOutputFile", &ppdev->OpenOutputFile)) < 0 ||
  476.     (code = param_write_bool(plist, "ReopenPerPage", &ppdev->ReopenPerPage)) < 0 ||
  477.     (ppdev->Duplex_set >= 0 &&
  478.      (code = (ppdev->Duplex_set ?
  479.           param_write_bool(plist, "Duplex", &ppdev->Duplex) :
  480.           param_write_null(plist, "Duplex"))) < 0)
  481.     )
  482.     return code;
  483.  
  484.     ofns.data = (const byte *)ppdev->fname,
  485.     ofns.size = strlen(ppdev->fname),
  486.     ofns.persistent = false;
  487.     return param_write_string(plist, "OutputFile", &ofns);
  488. }
  489.  
  490. /* Validate an OutputFile name by checking any %-formats. */
  491. private int
  492. validate_output_file(const gs_param_string * ofs)
  493. {
  494.     gs_parsed_file_name_t parsed;
  495.     const char *fmt;
  496.  
  497.     return gx_parse_output_file_name(&parsed, &fmt, (const char *)ofs->data,
  498.                      ofs->size) >= 0;
  499. }
  500.  
  501. /* Put parameters. */
  502. int
  503. gdev_prn_put_params(gx_device * pdev, gs_param_list * plist)
  504. {
  505.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  506.     int ecode = 0;
  507.     int code;
  508.     const char *param_name;
  509.     bool is_open = pdev->is_open;
  510.     bool oof = ppdev->OpenOutputFile;
  511.     bool rpp = ppdev->ReopenPerPage;
  512.     bool duplex;
  513.     int duplex_set = -1;
  514.     int width = pdev->width;
  515.     int height = pdev->height;
  516.     gdev_prn_space_params sp, save_sp;
  517.     gs_param_string ofs;
  518.     gs_param_dict mdict;
  519.  
  520.     sp = ppdev->space_params;
  521.     save_sp = sp;
  522.  
  523.     switch (code = param_read_bool(plist, (param_name = "OpenOutputFile"), &oof)) {
  524.     default:
  525.         ecode = code;
  526.         param_signal_error(plist, param_name, ecode);
  527.     case 0:
  528.     case 1:
  529.         break;
  530.     }
  531.  
  532.     switch (code = param_read_bool(plist, (param_name = "ReopenPerPage"), &rpp)) {
  533.     default:
  534.         ecode = code;
  535.         param_signal_error(plist, param_name, ecode);
  536.     case 0:
  537.     case 1:
  538.         break;
  539.     }
  540.  
  541.     if (ppdev->Duplex_set >= 0)    /* i.e., Duplex is supported */
  542.     switch (code = param_read_bool(plist, (param_name = "Duplex"),
  543.                        &duplex)) {
  544.         case 0:
  545.         duplex_set = 1;
  546.         break;
  547.         default:
  548.         if ((code = param_read_null(plist, param_name)) == 0) {
  549.             duplex_set = 0;
  550.             break;
  551.         }
  552.         ecode = code;
  553.         param_signal_error(plist, param_name, ecode);
  554.         case 1:
  555.         ;
  556.     }
  557. #define CHECK_PARAM_CASES(member, bad, label)\
  558.     case 0:\
  559.     if ((sp.params_are_read_only ? sp.member != save_sp.member : bad))\
  560.         ecode = gs_error_rangecheck;\
  561.     else\
  562.         break;\
  563.     goto label;\
  564.     default:\
  565.     ecode = code;\
  566. label:\
  567.     param_signal_error(plist, param_name, ecode);\
  568.     case 1:\
  569.     break
  570.  
  571.     switch (code = param_read_long(plist, (param_name = "MaxBitmap"), &sp.MaxBitmap)) {
  572.     CHECK_PARAM_CASES(MaxBitmap, sp.MaxBitmap < 10000, mbe);
  573.     }
  574.  
  575.     switch (code = param_read_long(plist, (param_name = "BufferSpace"), &sp.BufferSpace)) {
  576.     CHECK_PARAM_CASES(BufferSpace, sp.BufferSpace < 10000, bse);
  577.     }
  578.  
  579.     switch (code = param_read_int(plist, (param_name = "BandWidth"), &sp.band.BandWidth)) {
  580.     CHECK_PARAM_CASES(band.BandWidth, sp.band.BandWidth < 0, bwe);
  581.     }
  582.  
  583.     switch (code = param_read_int(plist, (param_name = "BandHeight"), &sp.band.BandHeight)) {
  584.     CHECK_PARAM_CASES(band.BandHeight, sp.band.BandHeight < 0, bhe);
  585.     }
  586.  
  587.     switch (code = param_read_long(plist, (param_name = "BandBufferSpace"), &sp.band.BandBufferSpace)) {
  588.     CHECK_PARAM_CASES(band.BandBufferSpace, sp.band.BandBufferSpace < 0, bbse);
  589.     }
  590.  
  591.     switch (code = param_read_string(plist, (param_name = "OutputFile"), &ofs)) {
  592.     case 0:
  593.         code = validate_output_file(&ofs);
  594.         if (code >= 0)
  595.         break;
  596.         /* falls through */
  597.     default:
  598.         ecode = code;
  599.         param_signal_error(plist, param_name, ecode);
  600.     case 1:
  601.         ofs.data = 0;
  602.         break;
  603.     }
  604.  
  605.     /* Read InputAttributes and OutputAttributes just for the type */
  606.     /* check and to indicate that they aren't undefined. */
  607. #define read_media(pname)\
  608.     switch ( code = param_begin_read_dict(plist, (param_name = pname), &mdict, true) )\
  609.       {\
  610.       case 0:\
  611.         param_end_read_dict(plist, pname, &mdict);\
  612.         break;\
  613.       default:\
  614.         ecode = code;\
  615.         param_signal_error(plist, param_name, ecode);\
  616.       case 1:\
  617.         ;\
  618.       }
  619.  
  620.     read_media("InputAttributes");
  621.     read_media("OutputAttributes");
  622.  
  623.     if (ecode < 0)
  624.     return ecode;
  625.     /* Prevent gx_default_put_params from closing the printer. */
  626.     pdev->is_open = false;
  627.     code = gx_default_put_params(pdev, plist);
  628.     pdev->is_open = is_open;
  629.     if (code < 0)
  630.     return code;
  631.  
  632.     ppdev->OpenOutputFile = oof;
  633.     ppdev->ReopenPerPage = rpp;
  634.     if (duplex_set >= 0) {
  635.     ppdev->Duplex = duplex;
  636.     ppdev->Duplex_set = duplex_set;
  637.     }
  638.     ppdev->space_params = sp;
  639.  
  640.     /* If necessary, free and reallocate the printer memory. */
  641.     /* Formerly, would not reallocate if device is not open: */
  642.     /* we had to patch this out (see News for 5.50). */
  643.     code = gdev_prn_maybe_realloc_memory(ppdev, &save_sp, width, height);
  644.     if (code < 0)
  645.     return code;
  646.  
  647.     /* If filename changed, close file. */
  648.     if (ofs.data != 0 &&
  649.     bytes_compare(ofs.data, ofs.size,
  650.               (const byte *)ppdev->fname, strlen(ppdev->fname))
  651.     ) {
  652.     /* Close the file if it's open. */
  653.     if (ppdev->file != NULL)
  654.         gx_device_close_output_file(pdev, ppdev->fname, ppdev->file);
  655.     ppdev->file = NULL;
  656.     memcpy(ppdev->fname, ofs.data, ofs.size);
  657.     ppdev->fname[ofs.size] = 0;
  658.     }
  659.     /* If the device is open and OpenOutputFile is true, */
  660.     /* open the OutputFile now.  (If the device isn't open, */
  661.     /* this will happen when it is opened.) */
  662.     if (pdev->is_open && oof) {
  663.     code = gdev_prn_open_printer(pdev, 1);
  664.     if (code < 0)
  665.         return code;
  666.     }
  667.     return 0;
  668. }
  669.  
  670. /* ------ Others ------ */
  671.  
  672. /* Default routine to (not) override current space_params. */
  673. void
  674. gx_default_get_space_params(const gx_device_printer *printer_dev,
  675.                 gdev_prn_space_params *space_params)
  676. {
  677.     return;
  678. }
  679.  
  680. /* Generic routine to send the page to the printer. */
  681. int    /* 0 ok, -ve error, or 1 if successfully upgraded to buffer_page */
  682. gdev_prn_output_page(gx_device * pdev, int num_copies, int flush)
  683. {
  684.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  685.     int outcode = 0, closecode = 0, errcode = 0, endcode;
  686.     bool upgraded_copypage = false;
  687.  
  688.     if (num_copies > 0 || !flush) {
  689.     int code = gdev_prn_open_printer(pdev, 1);
  690.  
  691.     if (code < 0)
  692.         return code;
  693.  
  694.     /* If copypage request, try to do it using buffer_page */
  695.     if ( !flush &&
  696.          (*ppdev->printer_procs.buffer_page)
  697.          (ppdev, ppdev->file, num_copies) >= 0
  698.          ) {
  699.         upgraded_copypage = true;
  700.         flush = true;
  701.     }
  702.     else if (num_copies > 0)
  703.         /* Print the accumulated page description. */
  704.         outcode =
  705.         (*ppdev->printer_procs.print_page_copies)(ppdev, ppdev->file,
  706.                               num_copies);
  707.     fflush(ppdev->file);
  708.     errcode =
  709.         (ferror(ppdev->file) ? gs_note_error(gs_error_ioerror) : 0);
  710.     if (!upgraded_copypage)
  711.         closecode = gdev_prn_close_printer(pdev);
  712.     }
  713.     endcode = (ppdev->buffer_space && !ppdev->is_async_renderer ?
  714.            clist_finish_page(pdev, flush) : 0);
  715.  
  716.     if (outcode < 0)
  717.     return outcode;
  718.     if (errcode < 0)
  719.     return errcode;
  720.     if (closecode < 0)
  721.     return closecode;
  722.     if (endcode < 0)
  723.     return endcode;
  724.     endcode = gx_finish_output_page(pdev, num_copies, flush);
  725.     return (endcode < 0 ? endcode : upgraded_copypage ? 1 : 0);
  726. }
  727.  
  728. /* Print a single copy of a page by calling print_page_copies. */
  729. int
  730. gx_print_page_single_copy(gx_device_printer * pdev, FILE * prn_stream)
  731. {
  732.     return pdev->printer_procs.print_page_copies(pdev, prn_stream, 1);
  733. }
  734.  
  735. /* Print multiple copies of a page by calling print_page multiple times. */
  736. int
  737. gx_default_print_page_copies(gx_device_printer * pdev, FILE * prn_stream,
  738.                  int num_copies)
  739. {
  740.     int i = 1;
  741.     int code = 0;
  742.  
  743.     for (; i < num_copies; ++i) {
  744.     int errcode, closecode;
  745.  
  746.     code = (*pdev->printer_procs.print_page) (pdev, prn_stream);
  747.     if (code < 0)
  748.         return code;
  749.     /*
  750.      * Close and re-open the printer, to reset is_new and do the
  751.      * right thing if we're producing multiple output files.
  752.      * Code is mostly copied from gdev_prn_output_page.
  753.      */
  754.     fflush(pdev->file);
  755.     errcode =
  756.         (ferror(pdev->file) ? gs_note_error(gs_error_ioerror) : 0);
  757.     closecode = gdev_prn_close_printer((gx_device *)pdev);
  758.     pdev->PageCount++;
  759.     code = (errcode < 0 ? errcode : closecode < 0 ? closecode :
  760.         gdev_prn_open_printer((gx_device *)pdev, true));
  761.     if (code < 0) {
  762.         pdev->PageCount -= i;
  763.         return code;
  764.     }
  765.     prn_stream = pdev->file;
  766.     }
  767.     /* Print the last (or only) page. */
  768.     pdev->PageCount -= num_copies - 1;
  769.     return (*pdev->printer_procs.print_page) (pdev, prn_stream);
  770. }
  771.  
  772. /*
  773.  * Buffer a (partial) rasterized page & optionally print result multiple times.
  774.  * The default implementation returns error, since the driver needs to override
  775.  * this (in procedure vector) in configurations where this call may occur.
  776.  */
  777. int
  778. gx_default_buffer_page(gx_device_printer *pdev, FILE *prn_stream,
  779.                int num_copies)
  780. {
  781.     return gs_error_unknownerror;
  782. }
  783.  
  784. /* ---------------- Driver services ---------------- */
  785.  
  786. /* Initialize a rendering plane specification. */
  787. int
  788. gx_render_plane_init(gx_render_plane_t *render_plane, const gx_device *dev,
  789.              int index)
  790. {
  791.     /*
  792.      * Eventually the computation of shift and depth from dev and index
  793.      * will be made device-dependent.
  794.      */
  795.     int num_planes = dev->color_info.num_components;
  796.     int plane_depth = dev->color_info.depth / num_planes;
  797.  
  798.     if (index < 0 || index >= num_planes)
  799.     return_error(gs_error_rangecheck);
  800.     render_plane->index = index;
  801.     render_plane->depth = plane_depth;
  802.     render_plane->shift = plane_depth * (num_planes - 1 - index);
  803.     return 0;
  804. }
  805.  
  806. /* Clear trailing bits in the last byte of (a) scan line(s). */
  807. void
  808. gdev_prn_clear_trailing_bits(byte *data, uint raster, int height,
  809.                  const gx_device *dev)
  810. {
  811.     int first_bit = dev->width * dev->color_info.depth;
  812.  
  813.     if (first_bit & 7)
  814.     bits_fill_rectangle(data, first_bit, raster, mono_fill_make_pattern(0),
  815.                 -first_bit & 7, height);
  816. }
  817.  
  818. /* Return the number of scan lines that should actually be passed */
  819. /* to the device. */
  820. int
  821. gdev_prn_print_scan_lines(gx_device * pdev)
  822. {
  823.     int height = pdev->height;
  824.     gs_matrix imat;
  825.     float yscale;
  826.     int top, bottom, offset, end;
  827.  
  828.     (*dev_proc(pdev, get_initial_matrix)) (pdev, &imat);
  829.     yscale = imat.yy * 72.0;    /* Y dpi, may be negative */
  830.     top = (int)(dev_t_margin(pdev) * yscale);
  831.     bottom = (int)(dev_b_margin(pdev) * yscale);
  832.     offset = (int)(dev_y_offset(pdev) * yscale);
  833.     if (yscale < 0) {        /* Y=0 is top of page */
  834.     end = -offset + height + bottom;
  835.     } else {            /* Y=0 is bottom of page */
  836.     end = offset + height - top;
  837.     }
  838.     return min(height, end);
  839. }
  840.  
  841. /* Open the current page for printing. */
  842. int
  843. gdev_prn_open_printer_seekable(gx_device *pdev, bool binary_mode,
  844.                    bool seekable)
  845. {
  846.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  847.  
  848.     if (ppdev->file != 0) {
  849.     ppdev->file_is_new = false;
  850.     return 0;
  851.     }
  852.     {
  853.     int code = gx_device_open_output_file(pdev, ppdev->fname,
  854.                           binary_mode, seekable,
  855.                           &ppdev->file);
  856.     if (code < 0)
  857.         return code;
  858.     }
  859.     ppdev->file_is_new = true;
  860.     return 0;
  861. }
  862. int
  863. gdev_prn_open_printer(gx_device *pdev, bool binary_mode)
  864. {
  865.     return gdev_prn_open_printer_seekable(pdev, binary_mode, false);
  866. }
  867.  
  868. /*
  869.  * Test whether the printer's output file was just opened, i.e., whether
  870.  * this is the first page being written to this file.  This is only valid
  871.  * at the entry to a driver's print_page procedure.
  872.  */
  873. bool
  874. gdev_prn_file_is_new(const gx_device_printer *pdev)
  875. {
  876.     return pdev->file_is_new;
  877. }
  878.  
  879. /* Determine the colors used in a range of lines. */
  880. int
  881. gx_page_info_colors_used(const gx_device *dev,
  882.              const gx_band_page_info_t *page_info,
  883.              int y, int height,
  884.              gx_colors_used_t *colors_used, int *range_start)
  885. {
  886.     int start, end, i;
  887.     int num_lines = page_info->scan_lines_per_colors_used;
  888.     gx_color_index or = 0;
  889.     bool slow_rop = false;
  890.  
  891.     if (y < 0 || height < 0 || height > dev->height - y)
  892.     return -1;
  893.     start = y / num_lines;
  894.     end = (y + height + num_lines - 1) / num_lines;
  895.     for (i = start; i < end; ++i) {
  896.     or |= page_info->band_colors_used[i].or;
  897.     slow_rop |= page_info->band_colors_used[i].slow_rop;
  898.     }
  899.     colors_used->or = or;
  900.     colors_used->slow_rop = slow_rop;
  901.     *range_start = start * num_lines;
  902.     return min(end * num_lines, dev->height) - *range_start;
  903. }
  904. int
  905. gdev_prn_colors_used(gx_device *dev, int y, int height,
  906.              gx_colors_used_t *colors_used, int *range_start)
  907. {
  908.     gx_device_clist_writer *cldev;
  909.  
  910.     /* If this isn't a banded device, return default values. */
  911.     if (dev_proc(dev, get_bits_rectangle) !=
  912.       gs_clist_device_procs.get_bits_rectangle
  913.     ) {
  914.     *range_start = 0;
  915.     colors_used->or = ((gx_color_index)1 << dev->color_info.depth) - 1;
  916.     return dev->height;
  917.     }
  918.     cldev = (gx_device_clist_writer *)dev;
  919.     if (cldev->page_info.scan_lines_per_colors_used == 0) /* not set yet */
  920.     clist_compute_colors_used(cldev);
  921.     return
  922.     gx_page_info_colors_used(dev, &cldev->page_info,
  923.                  y, height, colors_used, range_start);
  924. }
  925.  
  926. /*
  927.  * Create the buffer device for a printer device.  Clients should always
  928.  * call this, and never call the device procedure directly.
  929.  */
  930. int
  931. gdev_create_buf_device(create_buf_device_proc_t cbd_proc, gx_device **pbdev,
  932.                gx_device *target,
  933.                const gx_render_plane_t *render_plane,
  934.                gs_memory_t *mem, bool for_band)
  935. {
  936.     int code = cbd_proc(pbdev, target, render_plane, mem, for_band);
  937.  
  938.     if (code < 0)
  939.     return code;
  940.     /* Retain this device -- it will be freed explicitly. */
  941.     gx_device_retain(*pbdev, true);
  942.     return code;
  943. }
  944.  
  945. /*
  946.  * Create an ordinary memory device for page or band buffering,
  947.  * possibly preceded by a plane extraction device.
  948.  */
  949. int
  950. gx_default_create_buf_device(gx_device **pbdev, gx_device *target,
  951.     const gx_render_plane_t *render_plane, gs_memory_t *mem, bool for_band)
  952. {
  953.     int plane_index = (render_plane ? render_plane->index : -1);
  954.     int depth;
  955.     const gx_device_memory *mdproto;
  956.     gx_device_memory *mdev;
  957.     gx_device *bdev;
  958.  
  959.     if (plane_index >= 0)
  960.     depth = render_plane->depth;
  961.     else
  962.     depth = target->color_info.depth;
  963.     mdproto = gdev_mem_device_for_bits(depth);
  964.     if (mdproto == 0)
  965.     return_error(gs_error_rangecheck);
  966.     if (mem) {
  967.     mdev = gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  968.                    "create_buf_device");
  969.     if (mdev == 0)
  970.         return_error(gs_error_VMerror);
  971.     } else {
  972.     mdev = (gx_device_memory *)*pbdev;
  973.     }
  974.     if (target == (gx_device *)mdev) {
  975.     /* The following is a special hack for setting up printer devices. */
  976.     assign_dev_procs(mdev, mdproto);
  977.     } else
  978.     gs_make_mem_device(mdev, mdproto, mem, (for_band ? 1 : 0),
  979.                (target == (gx_device *)mdev ? NULL : target));
  980.     mdev->width = target->width;
  981.     /*
  982.      * The matrix in the memory device is irrelevant,
  983.      * because all we do with the device is call the device-level
  984.      * output procedures, but we may as well set it to
  985.      * something halfway reasonable.
  986.      */
  987.     gs_deviceinitialmatrix(target, &mdev->initial_matrix);
  988.     if (plane_index >= 0) {
  989.     gx_device_plane_extract *edev =
  990.         gs_alloc_struct(mem, gx_device_plane_extract,
  991.                 &st_device_plane_extract, "create_buf_device");
  992.  
  993.     if (edev == 0) {
  994.         gx_default_destroy_buf_device((gx_device *)mdev);
  995.         return_error(gs_error_VMerror);
  996.     }
  997.     edev->memory = mem;
  998.     plane_device_init(edev, target, (gx_device *)mdev, render_plane,
  999.               false);
  1000.     bdev = (gx_device *)edev;
  1001.     } else
  1002.     bdev = (gx_device *)mdev;
  1003.     /****** QUESTIONABLE, BUT BETTER THAN OMITTING ******/
  1004.     bdev->color_info = target->color_info;
  1005.     *pbdev = bdev;
  1006.     return 0;
  1007. }
  1008.  
  1009. /* Determine the space needed by the buffer device. */
  1010. int
  1011. gx_default_size_buf_device(gx_device_buf_space_t *space, gx_device *target,
  1012.                const gx_render_plane_t *render_plane,
  1013.                int height, bool for_band)
  1014. {
  1015.     gx_device_memory mdev;
  1016.  
  1017.     mdev.color_info.depth =
  1018.     (render_plane && render_plane->index >= 0 ? render_plane->depth :
  1019.      target->color_info.depth);
  1020.     mdev.width = target->width;
  1021.     mdev.num_planes = 0;
  1022.     space->bits = gdev_mem_bits_size(&mdev, target->width, height);
  1023.     space->line_ptrs = gdev_mem_line_ptrs_size(&mdev, target->width, height);
  1024.     space->raster = gdev_mem_raster(&mdev);
  1025.     return 0;
  1026. }
  1027.  
  1028. /* Set up the buffer device. */
  1029. int
  1030. gx_default_setup_buf_device(gx_device *bdev, byte *buffer, int bytes_per_line,
  1031.                 byte **line_ptrs, int y, int setup_height,
  1032.                 int full_height)
  1033. {
  1034.     gx_device_memory *mdev =
  1035.     (gs_device_is_memory(bdev) ? (gx_device_memory *)bdev :
  1036.      (gx_device_memory *)(((gx_device_plane_extract *)bdev)->plane_dev));
  1037.     byte **ptrs = line_ptrs;
  1038.     int raster = bytes_per_line;
  1039.     int code;
  1040.  
  1041.     /****** HACK ******/
  1042.     if ((gx_device *)mdev == bdev && mdev->num_planes)
  1043.     raster = bitmap_raster(mdev->planes[0].depth * mdev->width);
  1044.     if (ptrs == 0) {
  1045.     /*
  1046.      * Allocate line pointers now; free them when we close the device.
  1047.      * Note that for multi-planar devices, we have to allocate using
  1048.      * full_height rather than setup_height.
  1049.      */
  1050.     ptrs = (byte **)
  1051.         gs_alloc_byte_array(mdev->memory,
  1052.                 (mdev->num_planes ?
  1053.                  full_height * mdev->num_planes :
  1054.                  setup_height),
  1055.                 sizeof(byte *), "setup_buf_device");
  1056.     if (ptrs == 0)
  1057.         return_error(gs_error_VMerror);
  1058.     mdev->line_pointer_memory = mdev->memory;
  1059.     mdev->foreign_line_pointers = false;
  1060.     }
  1061.     mdev->height = full_height;
  1062.     code = gdev_mem_set_line_ptrs(mdev, buffer + raster * y, bytes_per_line,
  1063.                   ptrs, setup_height);
  1064.     mdev->height = setup_height;
  1065.     bdev->height = setup_height; /* do here in case mdev == bdev */
  1066.     return code;
  1067. }
  1068.  
  1069. /* Destroy the buffer device. */
  1070. void
  1071. gx_default_destroy_buf_device(gx_device *bdev)
  1072. {
  1073.     gx_device *mdev = bdev;
  1074.  
  1075.     if (!gs_device_is_memory(bdev)) {
  1076.     /* bdev must be a plane extraction device. */
  1077.     mdev = ((gx_device_plane_extract *)bdev)->plane_dev;
  1078.     gs_free_object(bdev->memory, bdev, "destroy_buf_device");
  1079.     }
  1080.     dev_proc(mdev, close_device)(mdev);
  1081.     gs_free_object(mdev->memory, mdev, "destroy_buf_device");
  1082. }
  1083.  
  1084. /*
  1085.  * Copy one or more rasterized scan lines to a buffer, or return a pointer
  1086.  * to them.  See gdevprn.h for detailed specifications.
  1087.  */
  1088. int
  1089. gdev_prn_get_lines(gx_device_printer *pdev, int y, int height,
  1090.            byte *buffer, uint bytes_per_line,
  1091.            byte **actual_buffer, uint *actual_bytes_per_line,
  1092.            const gx_render_plane_t *render_plane)
  1093. {
  1094.     int code;
  1095.     gs_int_rect rect;
  1096.     gs_get_bits_params_t params;
  1097.     int plane;
  1098.  
  1099.     if (y < 0 || height < 0 || y + height > pdev->height)
  1100.     return_error(gs_error_rangecheck);
  1101.     rect.p.x = 0, rect.p.y = y;
  1102.     rect.q.x = pdev->width, rect.q.y = y + height;
  1103.     params.options =
  1104.     GB_RETURN_POINTER | GB_ALIGN_STANDARD | GB_OFFSET_0 |
  1105.     GB_RASTER_ANY |
  1106.     /* No depth specified, we always use native colors. */
  1107.     GB_COLORS_NATIVE | GB_ALPHA_NONE;
  1108.     if (render_plane) {
  1109.     params.options |= GB_PACKING_PLANAR | GB_SELECT_PLANES;
  1110.     memset(params.data, 0,
  1111.            sizeof(params.data[0]) * pdev->color_info.num_components);
  1112.     plane = render_plane->index;
  1113.     params.data[plane] = buffer;
  1114.     } else {
  1115.     params.options |= GB_PACKING_CHUNKY;
  1116.     params.data[0] = buffer;
  1117.     plane = 0;
  1118.     }
  1119.     params.x_offset = 0;
  1120.     params.raster = bytes_per_line;
  1121.     code = dev_proc(pdev, get_bits_rectangle)
  1122.     ((gx_device *)pdev, &rect, ¶ms, NULL);
  1123.     if (code < 0 && actual_buffer) {
  1124.     /*
  1125.      * RETURN_POINTER might not be implemented for this
  1126.      * combination of parameters: try RETURN_COPY.
  1127.      */
  1128.     params.options &= ~(GB_RETURN_POINTER | GB_RASTER_ALL);
  1129.     params.options |= GB_RETURN_COPY | GB_RASTER_SPECIFIED;
  1130.     code = dev_proc(pdev, get_bits_rectangle)
  1131.         ((gx_device *)pdev, &rect, ¶ms, NULL);
  1132.     }
  1133.     if (code < 0)
  1134.     return code;
  1135.     if (actual_buffer)
  1136.     *actual_buffer = params.data[plane];
  1137.     if (actual_bytes_per_line)
  1138.     *actual_bytes_per_line = params.raster;
  1139.     return code;
  1140. }
  1141.  
  1142.  
  1143. /* Copy a scan line from the buffer to the printer. */
  1144. int
  1145. gdev_prn_get_bits(gx_device_printer * pdev, int y, byte * str, byte ** actual_data)
  1146. {
  1147.     int code = (*dev_proc(pdev, get_bits)) ((gx_device *) pdev, y, str, actual_data);
  1148.     uint line_size = gdev_prn_raster(pdev);
  1149.     int last_bits = -(pdev->width * pdev->color_info.depth) & 7;
  1150.  
  1151.     if (code < 0)
  1152.     return code;
  1153.     if (last_bits != 0) {
  1154.     byte *dest = (actual_data != 0 ? *actual_data : str);
  1155.  
  1156.     dest[line_size - 1] &= 0xff << last_bits;
  1157.     }
  1158.     return 0;
  1159. }
  1160. /* Copy scan lines to a buffer.  Return the number of scan lines, */
  1161. /* or <0 if error.  This procedure is DEPRECATED. */
  1162. int
  1163. gdev_prn_copy_scan_lines(gx_device_printer * pdev, int y, byte * str, uint size)
  1164. {
  1165.     uint line_size = gdev_prn_raster(pdev);
  1166.     int count = size / line_size;
  1167.     int i;
  1168.     byte *dest = str;
  1169.  
  1170.     count = min(count, pdev->height - y);
  1171.     for (i = 0; i < count; i++, dest += line_size) {
  1172.     int code = gdev_prn_get_bits(pdev, y + i, dest, NULL);
  1173.  
  1174.     if (code < 0)
  1175.         return code;
  1176.     }
  1177.     return count;
  1178. }
  1179.  
  1180. /* Close the current page. */
  1181. int
  1182. gdev_prn_close_printer(gx_device * pdev)
  1183. {
  1184.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  1185.     gs_parsed_file_name_t parsed;
  1186.     const char *fmt;
  1187.     int code = gx_parse_output_file_name(&parsed, &fmt, ppdev->fname,
  1188.                      strlen(ppdev->fname));
  1189.  
  1190.     if ((code >= 0 && fmt) /* file per page */ ||
  1191.     ppdev->ReopenPerPage    /* close and reopen for each page */
  1192.     ) {
  1193.     gx_device_close_output_file(pdev, ppdev->fname, ppdev->file);
  1194.     ppdev->file = NULL;
  1195.     }
  1196.     return 0;
  1197. }
  1198.  
  1199. /* If necessary, free and reallocate the printer memory after changing params */
  1200. int
  1201. gdev_prn_maybe_realloc_memory(gx_device_printer *prdev, 
  1202.                   gdev_prn_space_params *old_sp,
  1203.                   int old_width, int old_height)
  1204. {
  1205.     int code = 0;
  1206.     gx_device *const pdev = (gx_device *)prdev;
  1207.     /*gx_device_memory * const mdev = (gx_device_memory *)prdev;*/
  1208.     
  1209.     /*
  1210.      * The first test was changed to mdev->base != 0 in 5.50 (per Artifex).
  1211.      * Not only was this test wrong logically, it was incorrect in that
  1212.      * casting pdev to a (gx_device_memory *) is only meaningful if banding
  1213.      * is not being used.  The test was changed back to prdev->is_open in
  1214.      * 5.67 (also per Artifex).  For more information, see the News items
  1215.      * for these filesets.
  1216.      */
  1217.     if (prdev->is_open &&
  1218.     (memcmp(&prdev->space_params, old_sp, sizeof(*old_sp)) != 0 ||
  1219.      prdev->width != old_width || prdev->height != old_height )
  1220.     ) {
  1221.     int new_width = prdev->width;
  1222.     int new_height = prdev->height;
  1223.     gdev_prn_space_params new_sp;
  1224.  
  1225. #ifdef DEBUGGING_HACKS
  1226. debug_dump_bytes((const byte *)old_sp, (const byte *)(old_sp + 1), "old");
  1227. debug_dump_bytes((const byte *)&prdev->space_params,
  1228.          (const byte *)(&prdev->space_params + 1), "new");
  1229. dprintf4("w=%d/%d, h=%d/%d\n", old_width, new_width, old_height, new_height);
  1230. #endif /*DEBUGGING_HACKS*/
  1231.     new_sp = prdev->space_params;
  1232.     prdev->width = old_width;
  1233.     prdev->height = old_height;
  1234.     prdev->space_params = *old_sp;
  1235.     code = gdev_prn_reallocate_memory(pdev, &new_sp,
  1236.                       new_width, new_height);
  1237.     /* If this fails, device should be usable w/old params, but */
  1238.     /* band files may not be open. */
  1239.     }
  1240.     return code;
  1241. }
  1242.